# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from jms.dm.route.dm_route_whole_base_dt.dongbei import dm__dm_route_whole_dongbei_base
from utils.operators.rich_sql_sensor import RichSqlSensor
from utils.alerts.doris_telnet_check import net_is_used

label = f"dwd_route_whole_from_whole_dongbei_{int(time())}"
timeout = timedelta(hours=2).seconds
dorisConn = net_is_used()
doris_jms_dwd__dwd_route_whole_from_whole_dongbei = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_route_whole_from_whole_dongbei',
    pool='broker_load_pool',
    email=['zhangqinglin@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='doris',
    pre_sql=f"""
             LOAD LABEL jms_dwd.{label} (
                 DATA INFILE('hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_route_whole_base/dt={{{{ execution_date | cst_ds }}}}/region=dongbei/*')
                 INTO TABLE dwd_route_whole_bck
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_route_brokers | random_choice }}}}' 
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: (r[2] == 'FINISHED'),
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    #execution_timeout=timedelta(seconds=timeout + 120)
    #excel平均时长:10分3秒
    execution_timeout = timedelta(minutes=45), )

doris_jms_dwd__dwd_route_whole_from_whole_dongbei << dm__dm_route_whole_dongbei_base
